/******************************************************************************
 * SAGE - Scalable Adaptive Graphics Environment
 *
 * Module: sageSynchronizer.h
 * Author : Hyejung Hur, 
 * Description : This file contains the class which provides the syncing
 *               mechanism. 
 *
 * Copyright (C) 2004 Electronic Visualization Laboratory,
 * University of Illinois at Chicago
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either Version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License along
 * with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Direct questions, comments etc about SAGE to bijeong@evl.uic.edu or 
 * http://www.evl.uic.edu/cavern/forum/
 *
 *****************************************************************************/

#ifndef _SAGESYNCHRONIZER_H
#define _SAGESYNCHRONIZER_H

#include <vector>

#include "sageBase.h" 
#include "streamProtocol.h"

class sageSyncInfo {
public:
	int m_sourceId;
	int m_bufferId;
	int m_keyframeNum;
	sageSyncType m_syncType;	
	bool m_updateFlag;
	int	m_clientSockFd;	
	int m_passNum;
	bool m_lockFlag;
	sageSyncInfo() : m_sourceId(-1), m_bufferId(-1), m_keyframeNum(30), m_syncType(SAGE_SYNC_NONE),
	                 m_updateFlag(false), m_clientSockFd(-1), m_passNum(0), m_lockFlag(false) {}; 
	~sageSyncInfo() {};
};

class sageSynchronizer {
public:
	sageSynchronizer(bool audioRecv=false);
	~sageSynchronizer();

public:
	int init(int port);
	int connect(char* ip); 
	
	/** register to buffer information for contol
	*/
	void registerBuffer(sageSyncType type, int sourceId, int bufferId, int keyframeNum);

	/** remove buffer from the contorl list.
	*/
	int removeBuffer(int bufferId);


	/** check current frame number is keyframe 
	*/
	bool checkKeyFrame(int bufferId, int frameNum);
	
	/** check signal 
	*/
	bool checkSignal(int bufferId);
	
	int sendSignal(int bufferId);
	
	int checkConnections();
	
protected:
	int initListener(int port);
	int initSender(char* ip, int port);
	
	bool recvSignal(int socketFd, char* msg, int bufferId);
	int closeAll();

	int sendSignal(sageSyncInfo* info);	
	
	void doKeyFrameAction(sageSyncInfo* info);

protected:
	/** buffer list
	*/
	std::vector<sageSyncInfo*> m_syncInfoList;

	int			m_serverSockFd;
	int			m_clientSockFd;
	std::vector<int> m_clientSockList;
	
	sockaddr_in m_localAddr;
	sockaddr_in	m_serverAddr;
		
	int m_recvPort;
	int m_sendPort;
			
	// for identify receivers. : if it's aStreamRcv, true
	bool		m_audioReceiver;
	
	bool		m_connectedDone;
	// timer.... 
	
};

#endif
